﻿using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Du4
{
    public partial class BedenDagilimi : XPopupForm
    {
        int idx;
        public BedenDagilimi(int idx)
        {
            this.idx = idx;
            InitializeComponent();
        }

        BindingSource bs;
        string ignore = "modifiedon,idx,tip";
        DataTable dt = null;
        private void BedenDagilimi_Load(object sender, EventArgs e)
        {
            
            this.Text = "Beden Dağılımı";
            txtInfo.Caption = idx.ToString();  // + " | " + HeaderLabel.Text;

            DataSet ds = ExecuteDataSet("GetDu4KesilenAdet", idx);
            dt = ds.Tables[0];

            string colNames = "";


            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (ignore.Contains(dt.Columns[i].ColumnName.ToLower()))
                {
                    continue;
                }

                if (dt.Rows[0][i].ToString() == "0" || dt.Rows[0][i].ToString() == "")
                {
                    colNames += dt.Columns[i].ColumnName + ";";
                }
                else
                    break;
            }

            for (int i = dt.Columns.Count - 1; i > 0; i--)
            {
                if (ignore.Contains(dt.Columns[i].ColumnName.ToLower()))
                {
                    continue;
                }

                if (dt.Rows[0][i].ToString() == "0" || dt.Rows[0][i].ToString() == "")
                {
                    colNames += dt.Columns[i].ColumnName + ";";
                }
                else
                    break;
            }

            foreach (string colName in colNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                dt.Columns.Remove(colName);
            }


            foreach (DataColumn dc in dt.Columns)
            {
                string dcName = dc.ColumnName.ToLower();
                if (!ignore.Contains(dcName))
                    dc.Caption = dcName.Substring(1, dcName.Length - 1);
            }

            bs = new BindingSource(ds, dt.TableName);
            InitGridView(gv, bs);

            Font FormFont = new Font("Calibri", 24, FontStyle.Regular);
            Font HeaderFont = new Font("Calibri", 24, FontStyle.Regular);


            //this.panelControl1.Visible = false;

            gv.OptionsView.ShowAutoFilterRow = false;
            gv.RowHeight = 40;
            gv.OptionsBehavior.ReadOnly = true;
            gv.OptionsBehavior.Editable = false;
            gv.OptionsView.ShowIndicator = false;
            gv.Appearance.HeaderPanel.Font = HeaderFont;
            gv.Appearance.Row.Font = FormFont;
            gv.OptionsSelection.UseIndicatorForSelection = false;
            gv.OptionsSelection.MultiSelect = false;
            gv.OptionsSelection.EnableAppearanceFocusedCell = true;
            gv.OptionsSelection.EnableAppearanceFocusedRow = false;
            gv.OptionsView.EnableAppearanceOddRow = false;
            gv.OptionsView.EnableAppearanceEvenRow = false;
            gv.Appearance.FocusedCell.BackColor = Color.YellowGreen;
            gv.BestFitColumns();
            gv.Columns["idx"].Visible = false;
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            DataRow dr = gv.GetFocusedDataRow();
            object cellValue = dr[gv.FocusedColumn.FieldName];
            int i = 0;
            if (cellValue != DBNull.Value)
            {
                i = (int)cellValue; ;
            }

            dr[gv.FocusedColumn.FieldName] = i + 1;
            gv.BestFitColumns();

        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            DataRow dr = gv.GetFocusedDataRow();
            object cellValue = dr[gv.FocusedColumn.FieldName];
            int i = 0;
            if (cellValue != DBNull.Value)
            {
                i = (int)cellValue; ;
            }
            if (i == 0)
                return;

            dr[gv.FocusedColumn.FieldName] = i - 1;
            gv.BestFitColumns();
        }

        private void gv_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle == 0)
            {
                gv.FocusedRowHandle = 1;
            }
            if (ignore.Contains(e.Column.Name.ToLower()))
            {
                gv.FocusedColumn = gv.Columns[1];
            }
        }


        private void btnOk_Click(object sender, EventArgs e)
        {
            string up1 = string.Format("if not exists(select 1 from DU4KesilenAdet where idx = {0}) insert into DU4KesilenAdet(idx) values ({0})   update Du4KesilenAdet set ", idx);
            string up2 = "";

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                DataColumn dc = dt.Columns[i];
                DataRow dr = dt.Rows[1];
                string colName = dc.ColumnName;

                if (ignore.Contains(colName.ToLower())) continue;
                if (dr[colName].ToString() == "") continue;

                up2 += colName + " = " + dr[colName].ToString() + ", ";
            }
            up2 = up2.Remove(up2.Length - 2);
            up2 = up1 + up2 + " where idx = " + idx;

            Session.DataManager.ExecuteDataTableByQuery(up2);
            Session.DataManager.ExecuteNonQuery("UpdDu4BD", idx);
            ShowInfoMessage("Beden dağılımı başarılı bir şekilde girildi.");
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}